﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace Quanlybanhang
{
    public partial class MuavaoForm : Office2007Form
    {
        Boolean save = true;
        String diachi = "";
        int id_chuhang = 0;
        DateTime dt;
        public MuavaoForm()
        {
            InitializeComponent();
            lblNgaythang.Text = Datetime();
            khoitao();
        }
        
        //Phuong thuc lay ngay thang hien tai
        public String Datetime()
        {
            String time;
            dt = DateTime.Now;
            time = "Ngày " + dt.Day + " Tháng " + dt.Month + " Năm " + dt.Year;
            return time;
        }

        //Phuong thuc khoi tao cac gia tri ban dau
        public void khoitao()
        {
            //Dua vao cmbbox
            //Lay danh sach nhung chu hang ban hang - kieu = 2
            string cmb_cmd = "select distinct ten from chuhang where kieu = 2";
            SqlDataAdapter sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            cmbChuhang.DataSource = dt;
            cmbChuhang.DisplayMember = "ten";

            cmb_cmd = "select tenhang as 'Tên hàng', '' as 'Số lượng', gia as 'Đơn giá', '' as 'Thành tiền' from loaihang";
            sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt1 = new DataTable();
            sda.Fill(dt1);
            dataGridViewX1.DataSource = dt1;
        }

        //Phuong thuc xu li khi chon o combobox
        private void cmbChuhang_SelectedIndexChanged(object sender, EventArgs e)
        {
            String ten = ""; String sdt = "";
            Decimal tienno = 0;
            String chuhang = cmbChuhang.Text;
            String cmd = "select id, ten, diachi, dienthoai, sono from chuhang where ten = '" + chuhang + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                id_chuhang = (int)(rdr["id"]);
                ten = (string)(rdr["ten"]);
                diachi = (string)(rdr["diachi"]);
                sdt = (string)(rdr["dienthoai"]);
                tienno = (decimal)(rdr["sono"]);
            }
            lblThongtin.Text = "Thông tin: \t" + ten + " -- " + diachi + " -- " + sdt;
            txtNocu.Text = tienno.ToString();
            rdr.Close();
        }

        //Phuong thuc lay id cua loai hang
        private int getIdLoaihang(String tenhang)
        {
            int id = 0;
            String cmd = "select id from loaihang where tenhang = '" + tenhang + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                id = (int)(rdr["id"]);
            }
            rdr.Close();
            return id;
        }

        //Su kien nhan button tinh tien
        private void btnTinhtien_Click(object sender, EventArgs e)
        {
            int numrow = dataGridViewX1.RowCount - 1;
            double soluong;
            double dongia;
            double tongtien = 0;

            for (int i = 0; i < numrow; i++)
            {
                try
                {
                    soluong = Convert.ToDouble(dataGridViewX1.Rows[i].Cells[1].FormattedValue.ToString());
                }
                catch (Exception ex)
                {
                    soluong = 0;
                    dataGridViewX1.Rows[i].Cells[1].Value = 0;
                }
                dongia = Convert.ToDouble(dataGridViewX1.Rows[i].Cells[2].FormattedValue.ToString());
                dataGridViewX1.Rows[i].Cells[3].Value = (soluong * dongia);
                tongtien += (soluong * dongia);
            }

            txtTongtien.Text = tongtien.ToString();
            save = false;
        }

        //Su kien nhan button tinh tien cuoi
        private void btnTinhtiencuoi_Click(object sender, EventArgs e)
        {
            double tralannay = 0;
            double nocu = Convert.ToDouble(txtNocu.Text);
            double conno = 0;
            double tongtien = Convert.ToDouble(txtTongtien.Text);
            try
            {
                tralannay = Convert.ToDouble(txtTra.Text);
                conno = tongtien + nocu - tralannay;
                txtConno.Text = conno.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hãy nhập đúng tiền trả lần này");
            }
        }

        //Su kien khi form dang dong lai
        private void BanraForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (save == false)
            {
                DialogResult hoithoat = MessageBox.Show("Bạn chưa lưu vào cơ sở dữ liệu, chắc chắn muốn thoát?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (hoithoat == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        //Su kien nhan button xuat hoa don
        private void btnXuathoadon_Click(object sender, EventArgs e)
        {
            MuavaoReportForm mvrpf = new MuavaoReportForm();
            mvrpf.khachhang = cmbChuhang.Text;
            mvrpf.diachi = this.diachi;
            mvrpf.tongcong = txtTongtien.Text;
            mvrpf.nocu = txtNocu.Text;
            mvrpf.datra = txtTra.Text;
            mvrpf.conlai = txtConno.Text;
            mvrpf.ngaythang = lblNgaythang.Text;
            mvrpf.dt = dataGridViewX1.DataSource as DataTable;
            mvrpf.ShowDialog();
        }

        //Su kien khi nhan button luu lai
        private void btnLuulai_Click(object sender, EventArgs e)
        {
            //Cap nhat lai so no cho chu hang
            //SqlConnection sqlConn = new SqlConnection(connection string here);
            SqlCommand sqlComm = new SqlCommand();
            sqlComm = MainForm.con.CreateCommand();
            sqlComm.CommandText = @"UPDATE CHUHANG SET sono='" + txtConno.Text + "' WHERE id='" + id_chuhang + "'";
            sqlComm.ExecuteNonQuery();

            int numrow = dataGridViewX1.RowCount - 1;
            double soluong;
            double dongia;
            double tongtien = 0;
            string loaihang = "";

            for (int i = 0; i < numrow; i++)
            {
                soluong = Convert.ToDouble(dataGridViewX1.Rows[i].Cells[1].FormattedValue.ToString());
                dongia = Convert.ToDouble(dataGridViewX1.Rows[i].Cells[2].FormattedValue.ToString());
                loaihang = dataGridViewX1.Rows[i].Cells[0].FormattedValue.ToString();
                tongtien += (soluong * dongia);
                sqlComm.CommandText = @"INSERT INTO LUUTRU VALUES('" + dt.Day + "', '" + dt.Month + "', '" + dt.Year + "', '" + getIdLoaihang(loaihang) + "', '" + id_chuhang + "', '"+ soluong +"', '"+ tongtien +"', '1')";
                sqlComm.ExecuteNonQuery();
            }

            save = true;
            MessageBox.Show("Lưu lại thành công!");
        }
    }
}
